package com.yay.easter.graphics;

import java.io.IOException;
import java.io.InputStream;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.yay.easter.R;
import com.yay.easter.R.drawable;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.util.Log;


public class EllipseRenderer implements GLSurfaceView.Renderer {
	public float mAngleX;
	public float mAngleY;

	private FloatBuffer mFVertexBuffer;
	private ShortBuffer mIndexBuffer;
	private int numOfIndecies = 0;
	private int sides = 50;
	private float mRed;
    private float mGreen;
    private float mBlue;
    private int mTextureID;
	private int mTexture = R.drawable.t1;
	private boolean isDirty;
	private Context mContext;
	private boolean mTranslucentBackground;

	
	public EllipseRenderer(Context context, boolean useTranslucentBackground) {
		mContext = context;
		mTranslucentBackground = useTranslucentBackground;
		prepareBuffers(sides);
	}

	public int[] getConfigSpec() {
        int[] configSpec = {EGL10.EGL_DEPTH_SIZE, 0, EGL10.EGL_NONE};
        return configSpec;
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glClearColor(.5f, .5f, .5f, 1);
        //gl.glShadeModel(GL10.GL_SMOOTH);
        //gl.glEnable(GL10.GL_DEPTH_TEST);
        
        /*if (mTranslucentBackground) {
            gl.glClearColor(0,0,0,0);
        } else {
            gl.glClearColor(1,1,1,1);
        }*/
        //gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_TEXTURE_2D);

        int[] textures = new int[1];
        gl.glGenTextures(1, textures, 0);

        mTextureID = textures[0];
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
        gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);

        isDirty = true;
        //drawTexture();
    }
    
    public void onSurfaceChanged(GL10 gl, int w, int h) {
        gl.glViewport(0, 0, w, h);
        float ratio = (float) w / h;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        //gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    }
    
    public void onDrawFrame(GL10 gl) {
    	gl.glDisable(GL10.GL_DITHER);
        gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
        //gl.glClearColor(mRed, mGreen, mBlue, 1.0f);
    	gl.glClearColor(0f, 1.0f, 0f, 1.0f);
    	gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        
        gl.glTranslatef(0, 0, -3.0f);
        gl.glRotatef(mAngleX, 0, 1, 0);
        gl.glRotatef(mAngleY, 1, 0, 0);        
        //GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glActiveTexture(GL10.GL_TEXTURE0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
        gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
        //drawTexture();
        draw(gl);
        //gl.glRotatef(mAngleX*2.0f, 0, 1, 1);
        //gl.glTranslatef(0.5f, 0.5f, 0.5f);
        //mCube.draw(gl);
        //mAngleX += 1.2f;

        
        //GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        //gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //draw(gl);
    }
    
    public void setColor(float r, float g, float b) {
        mRed = r;
        mGreen = g;
        mBlue = b;
    }
    
    public void setTexture(int texture) {
    	mTexture = texture;
    	isDirty = true;
    	drawTexture();
    }



    // PRIVATE METHODS

	private void draw(GL10 gl) {
	    //gl.glColor4f(1.0f, 0, 0, 0.5f);
	    //gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
	    //gl.glDrawElements(GL10.GL_TRIANGLES, this.numOfIndecies, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
	    
	    gl.glFrontFace(GL10.GL_CCW);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mFVertexBuffer);
        //gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, VERTS,GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLES, this.numOfIndecies, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
	}

	private void prepareBuffers(int sides) {
	    Ellipse t = new Ellipse(0,0,0,1,sides);
	    this.mFVertexBuffer = t.getVertexBuffer();
	    this.mIndexBuffer = t.getIndexBuffer();
	    this.numOfIndecies = t.getNumberOfIndecies();
	    this.mFVertexBuffer.position(0);
	    this.mIndexBuffer.position(0);
	}

    private void drawTexture() {
    	if(isDirty) {
    		isDirty = false;
    		InputStream is = mContext.getResources().openRawResource(mTexture);
	        Bitmap bitmap;
	        try {
	            bitmap = BitmapFactory.decodeStream(is);
	        } finally {
	            try {
	                is.close();
	            } catch(IOException e) {
	                Log.e(this.getClass().getName(), e.toString());
	            }
	        }
	        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
	        bitmap.recycle();
    	}
    }
}
